/*
Copyright (c) 2012 - Dimok and giantpune

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.
*/
#include <cstring>
#include <malloc.h>

#include <ogc/machine/processor.h>

#include "gecko.h"
#include "sdcontentcrypto.h"
#include "sha1.h"

s32 ES_VerifySign( u8* sigHash,	u32 sigHashSize, u8* sig, u8* certs, u32 certLen )
{
	// check for NULL
	if( !sigHash || !sig || !certs || !certLen )
	{
		gprintf( "null shit\n" );
		return -1017;
	}

	// check alignment
	if( (((u32)sigHash) & 0x3f ) || (((u32)sig) & 0x1f ) || (((u32)certs) & 0x1f ) )
	{
		gprintf( "unaligned shit %p %p %p [%u %u %u]\n", sigHash, sig, certs,
				 (((u32)sigHash) & 0x3f ), (((u32)sig) & 0x1f ), (((u32)certs) & 0x1f ) );
		return -1017;
	}

	// libogc doesnt expose its es handle, so create our own
	char esPath[] __attribute__(( aligned( 32 ) )) = "/dev/es";
	s32 esFd = IOS_Open( esPath, 0 );
	if( esFd < 0 )
	{
		gprintf( "IOS_Open( \"/dev/es\" ): %i\n", esFd );
		return -1017;
	}

	// setup vector
	u32 vec[6]__attribute__(( aligned( 32 ) ));

	vec[0] = (u32)sigHash;
	vec[1] = sigHashSize;
	vec[2] = (u32)sig;
	vec[3] = 0x3c;// sig length
	vec[4] = (u32)certs;
	vec[5] = certLen;

	// send it
	s32 ret = IOS_Ioctlv( esFd, 0x31, 3, 0, (ioctlv*)(&vec) );

	// close /dev/es
	IOS_Close( esFd );

	/*
	  -1024	failure to heap alloc
	  -1027	invalid cert length
	  -2000	key creation failed

	  */

	return ret;
}

//! ipl::NandSDWorker::c_es_encrypt_iv
static u8 c_es_encrypt_iv[ 0x10 ] = { 0x21, 0x67, 0x12, 0xE6, 0xAA, 0x1F, 0x68, 0x9F, 0x95, 0xC5, 0xA2, 0x23, 0x24, 0xDC, 0x6A, 0x98 };

s32 DecryptWithSDKey( u8* in, u32 len, u8* out )
{
	u8 iv[ 0x10 ]__attribute__(( aligned( 32 ) ));
	memcpy( iv, c_es_encrypt_iv, 0x10 );

	return ES_Decrypt( ES_KEY_SDCARD, iv, in, len, out );
}

bool VerifyBKWad( u8* bkData, u32 len )
{
	u32 ret;
	STACK_ALIGN( u8, sha1Buf, 0x14, 0x40 );

	// check header
	BkHeader *bkHeader = (BkHeader*)( bkData );
	if( bkHeader->_0x70 != 0x70 || bkHeader->magic != 0x426b0001 )
	{
		gprintf( "bad bkheader\n" );
		return false;
	}

	// verify length
	u32 tSize = bkHeader->size2 - 0x340;

	if( len < bkHeader->size2 )
	{
		gprintf( "bad bkheader length\n" );
		return false;
	}

	// calc sha1
	GetSha1( (u8*)bkHeader, tSize, sha1Buf );

	// generate cert buffer
	u8* certBuf = (u8*)memalign( 0x20, 0xf80 );
	if( !certBuf )
	{
		gprintf( "!certBuf\n" );
		return false;
	}
	u8* sig = ((u8*)bkHeader) + tSize;
	memcpy( certBuf, ca_ppki, 0x400 );
	memcpy( certBuf + 0x400, ms_ppki, 0x240 );
	memcpy( certBuf + 0x640, ca_dpki, 0x400 );
	memcpy( certBuf + 0xa40, ms_dpki, 0x240 );
	memcpy( certBuf + 0xc80, sig + 0x40, 0x180 );
	memcpy( certBuf + 0xe00, sig + 0x40 + 0x180, 0x180 );
	DCFlushRange( certBuf, 0xf80 );

	// have IOS check the signature
	ret = ES_VerifySign( sha1Buf, 0x14, sig, certBuf, 0xf80 );

	// done with that
	free( certBuf );
	if( ret < 0 )
	{
		gprintf( "ES_VerifySign(): %i\n", ret );
		return false;
	}
	return true;
}


u8 ca_dpki[ 0x400 ] =
{ 0,
	/*0x00, 0x01, 0x00, 0x00, 0x90,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x44, 0x06, 0x6b, 0xa1, 0xf4, 0x58, 0xb6, 0x13, 0x40, 0x13, 0x8b, 0x03, 0x32, 0xa3, 0x10,
	0xb8, 0xd3, 0xc2, 0x3e, 0xac, 0x22, 0xb0, 0xd6, 0x06, 0xa9, 0x8f, 0x51, 0x98, 0xd8, 0xcf, 0x3a,
	0xc7, 0x63, 0x85, 0x04, 0xff, 0x0a, 0x02, 0xd1, 0x37, 0xaf, 0x4a, 0x7b, 0xb8, 0x91, 0x46, 0x95,
	0x25, 0xf3, 0x6c, 0xeb, 0xfd, 0xcb, 0x0c, 0xd7, 0xf2, 0xd5, 0xe7, 0x2e, 0xce, 0xdc, 0x26, 0xfe,
	0x5d, 0xb2, 0x0b, 0x71, 0xab, 0x5b, 0x47, 0xd3, 0xa4, 0xc9, 0xef, 0xeb, 0xf4, 0x2f, 0x48, 0x48,
	0xab, 0xf7, 0xf5, 0x63, 0x79, 0x97, 0x08, 0x8a, 0x4c, 0x75, 0x0d, 0xde, 0x29, 0x0f, 0x18, 0x77,
	0x8b, 0x2b, 0x8c, 0x3d, 0x60, 0x17, 0xeb, 0x2a, 0xda, 0x37, 0x4e, 0x1d, 0x17, 0xef, 0xf9, 0x98,
	0xe8, 0xbf, 0xb2, 0x53, 0xde, 0xe2, 0xf4, 0xb5, 0x63, 0x02, 0x62, 0x95, 0xeb, 0x89, 0x49, 0xb5,
	0xb4, 0xd1, 0x03, 0xa2, 0xf5, 0x00, 0x11, 0x5f, 0x45, 0xa1, 0xd0, 0xf2, 0x3b, 0x92, 0xc0, 0x34,
	0x06, 0xe4, 0xb6, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x52, 0x6f, 0x6f, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x65, 0x64, 0x8f, 0x2b, 0xc9, 0xcc, 0x2d, 0xc4, 0xdf, 0x29, 0x30, 0xe4,
	0xdf, 0x3f, 0x8c, 0x70, 0xa0, 0x78, 0x94, 0x87, 0x75, 0xad, 0x5e, 0x9a, 0xa6, 0x04, 0xc5, 0xb4,
	0xd8, 0xea, 0xff, 0x2a, 0xa1, 0xd2, 0x14, 0x67, 0x65, 0x64, 0xef, 0xca, 0x28, 0xcc, 0x00, 0x15,
	0x45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 0xd2, 0x88, 0xcb, 0xa4, 0x66, 0xc5, 0xfe, 0x5f,
	0x45, 0x4a, 0xe6, 0x79, 0x74, 0x4a, 0x90, 0xa1, 0x57, 0x72, 0xdb, 0x3b, 0x0e, 0x47, 0xa4, 0x9a,
	0xf0, 0x31, 0xd1, 0x6d, 0xbe, 0xab, 0x33, 0x2b, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00*/
};

u8 ca_ppki[ 0x400 ] =
{ 0,
		/*0x00, 0x01, 0x00, 0x00, 0xb3, 0xad, 0xb3, 0x22, 0x6b, 0x3c, 0x3d, 0xff, 0x1b, 0x4b, 0x40, 0x77,
		0x16, 0xff, 0x4f, 0x7a, 0xd7, 0x64, 0x86, 0xc8, 0x95, 0xac, 0x56, 0x2d, 0x21, 0xf1, 0x06, 0x01,
		0xd4, 0xf6, 0x64, 0x28, 0x19, 0x1c, 0x07, 0x76, 0x8f, 0xdf, 0x1a, 0xe2, 0xce, 0x7b, 0x27, 0xc9,
		0x0f, 0xbc, 0x0a, 0xd0, 0x31, 0x25, 0x78, 0xec, 0x07, 0x79, 0xb6, 0x57, 0xd4, 0x37, 0x24, 0x13,
		0xa7, 0xf8, 0x6f, 0x0c, 0x14, 0xc0, 0xef, 0x6e, 0x09, 0x41, 0xed, 0x2b, 0x05, 0xec, 0x39, 0x57,
		0x36, 0x07, 0x89, 0x00, 0x4a, 0x87, 0x8d, 0x2e, 0x9d, 0xf8, 0xc7, 0xa5, 0xa9, 0xf8, 0xca, 0x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, 0x8e, 0x56, 0x4f,
		0x5b, 0x89, 0xff, 0x37, 0x97, 0x37, 0xf1, 0xf5, 0x01, 0x3b, 0x1f, 0x9e, 0xc4, 0x18, 0x6f, 0x92,
		0x2a, 0xd5, 0xc4, 0xb3, 0xc0, 0xd5, 0x87, 0x0b, 0x9c, 0x04, 0xaf, 0x1a, 0xb5, 0xf3, 0xbc, 0x6d,
		0x0a, 0xf1, 0x7d, 0x47, 0x08, 0xe4, 0x43, 0xe9, 0x73, 0xf7, 0xb7, 0x70, 0x77, 0x54, 0xba, 0xf3,
		0xec, 0xd2, 0xac, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x52, 0x6f, 0x6f, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x01, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x5b, 0xfa, 0x7d, 0x5c, 0xb2, 0x79, 0xc9, 0xe2, 0xee, 0xe1, 0x21, 0xc6,
		0xea, 0xf4, 0x4f, 0xf6, 0x39, 0xf8, 0x8f, 0x07, 0x8b, 0x4b, 0x77, 0xed, 0x9f, 0x95, 0x60, 0xb0,
		0x35, 0x82, 0x81, 0xb5, 0x0e, 0x55, 0xab, 0x72, 0x11, 0x15, 0xa1, 0x77, 0x70, 0x3c, 0x7a, 0x30,
		0xfe, 0x3a, 0xe9, 0xef, 0x1c, 0x60, 0xbc, 0x1d, 0x97, 0x46, 0x76, 0xb2, 0x3a, 0x68, 0xcc, 0x04,
		0xb1, 0x98, 0x52, 0x5b, 0xc9, 0x68, 0xf1, 0x1d, 0xe2, 0xdb, 0x50, 0xe4, 0xd9, 0xe7, 0xf0, 0x71,
		0xe5, 0x62, 0xda, 0xe2, 0x09, 0x22, 0x33, 0xe9, 0xd3, 0x63, 0xf6, 0x1d, 0xd7, 0xc1, 0x9f, 0xf3,
		0xa4, 0xa9, 0x1e, 0x8f, 0x65, 0x53, 0xd4, 0x71, 0xdd, 0x7b, 0x84, 0xb9, 0xf1, 0xb8, 0xce, 0x73,
		0x35, 0xf0, 0xf5, 0x54, 0x05, 0x63, 0xa1, 0xea, 0xb8, 0x39, 0x63, 0xe0, 0x9b, 0xe9, 0x01, 0x01,
		0x1f, 0x99, 0x54, 0x63, 0x61, 0x28, 0x70, 0x20, 0xe9, 0xcc, 0x0d, 0xab, 0x48, 0x7f, 0x14, 0x0d,
		0x66, 0x26, 0xa1, 0x83, 0x6d, 0x27, 0x11, 0x1f, 0x20, 0x68, 0xde, 0x47, 0x72, 0x14, 0x91, 0x51,
		0xcf, 0x69, 0xc6, 0x1b, 0xa6, 0x0e, 0xf9, 0xd9, 0x49, 0xa0, 0xf7, 0x1f, 0x54, 0x99, 0xf2, 0xd3,
		0x9a, 0xd2, 0x8c, 0x70, 0x05, 0x34, 0x82, 0x93, 0xc4, 0x31, 0xff, 0xbd, 0x33, 0xf6, 0xbc, 0xa6,
		0x0d, 0xc7, 0x19, 0x5e, 0xa2, 0xbc, 0xc5, 0x6d, 0x20, 0x0b, 0xaf, 0x6d, 0x06, 0xd0, 0x9c, 0x41,
		0xdb, 0x8d, 0xe9, 0xc7, 0x20, 0x15, 0x4c, 0xa4, 0x83, 0x2b, 0x69, 0xc0, 0x8c, 0x69, 0xcd, 0x3b,
		0x07, 0x3a, 0x00, 0x63, 0x60, 0x2f, 0x46, 0x2d, 0x33, 0x80, 0x61, 0xa5, 0xea, 0x6c, 0x91, 0x5c,
		0xd5, 0x62, 0x35, 0x79, 0xc3, 0xeb, 0x64, 0xce, 0x44, 0xef, 0x58, 0x6d, 0x14, 0xba, 0xaa, 0x88,
		0x34, 0x01, 0x9b, 0x3e, 0xeb, 0xee, 0xd3, 0x79, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00*/
};

u8 ms_dpki[ 0x240 ] =
{ 0,
		/*0x00, 0x01, 0x00, 0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 0x22, 0x16, 0xa9, 0x3d, 0xd9, 0xec, 0x2d, 0xd6,
		0xc3, 0x10, 0x50, 0x15, 0xf6, 0x99, 0x8d, 0x65, 0xb5, 0x04, 0x76, 0x22, 0x1d, 0xca, 0x37, 0xe2,
		0x45, 0xc6, 0x60, 0x49, 0xa5, 0x54, 0x0a, 0x47, 0x5b, 0x63, 0x27, 0xd4, 0x97, 0x62, 0xad, 0x52,
		0x9e, 0x23, 0xc2, 0x74, 0xcf, 0x13, 0xfa, 0x78, 0x14, 0x0b, 0x6a, 0x8f, 0xfa, 0x57, 0xc0, 0xc6,
		0x95, 0x24, 0x83, 0x16, 0xfc, 0x19, 0xbc, 0x95, 0x2b, 0xd2, 0x4a, 0x35, 0x51, 0x83, 0x5a, 0xc7,
		0xc5, 0xa9, 0x5f, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x02, 0x4d, 0x53, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0xfc, 0xf5, 0xa9, 0xbc, 0x00, 0x8c, 0xf9, 0x5d, 0x1a, 0x80, 0xdb, 0x01,
		0xb2, 0xfd, 0x61, 0x68, 0xe0, 0x80, 0x4a, 0xaf, 0x95, 0xea, 0xf2, 0x75, 0x69, 0x6e, 0x40, 0x84,
		0x17, 0x2c, 0xce, 0x74, 0x42, 0x05, 0x00, 0x9b, 0xe0, 0x20, 0x0c, 0xc9, 0x47, 0x68, 0x49, 0x38,
		0xfb, 0x14, 0x54, 0x35, 0x46, 0x80, 0xab, 0xdb, 0xed, 0x0b, 0xbf, 0x49, 0xba, 0x82, 0xd0, 0x8a,
		0x8c, 0xa5, 0x71, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00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3c, 0x43, 0x6e, 0x3f, 0xc6, 0x63, 0xbb, 0x68, 0xa5, 0xc9, 0xeb, 0xb5, 0x8a, 0xa1, 0xe3, 0x86,
		0x50, 0x6c, 0xf2, 0x37, 0x2e, 0xea, 0xd9, 0xba, 0x28, 0x05, 0xb4, 0xfe, 0xef, 0xab, 0xcd, 0x7e,
		0xc6, 0x29, 0x82, 0xf7, 0x7d, 0x6f, 0x8e, 0x7f, 0xe5, 0x60, 0x1a, 0x9f, 0x2f, 0x5f, 0xaa, 0x65,
		0xdf, 0x34, 0x0f, 0xf8, 0x78, 0x0c, 0xfc, 0xb2, 0x77, 0x55, 0x4b, 0x07, 0x30, 0xe5, 0xb8, 0xaa,
		0xf5, 0xdd, 0xf4, 0x8e, 0x61, 0xce, 0x1b, 0x21, 0x41, 0xe3, 0xa5, 0xd1, 0xc8, 0xb3, 0x67, 0x50,
		0x7d, 0x61, 0x7b, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x02, 0x4d, 0x53, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0xf2, 0xa1, 0xf8, 0x12, 0x00, 0xfd, 0x56, 0x04, 0x18, 0x2c, 0xf1, 0x75,
		0x09, 0x21, 0x00, 0xc3, 0x08, 0xae, 0x48, 0x39, 0x91, 0x1b, 0x6f, 0x9f, 0xa1, 0xd5, 0x3a, 0x95,
		0xaf, 0x08, 0x33, 0x49, 0x47, 0x2b, 0x00, 0x01, 0x71, 0x31, 0x69, 0xb5, 0x91, 0xff, 0xd3, 0x0c,
		0xbf, 0x73, 0xda, 0x76, 0x64, 0xba, 0x8d, 0x0d, 0xf9, 0x5b, 0x4d, 0x11, 0x04, 0x44, 0x64, 0x35,
		0xc0, 0xed, 0xa4, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00*/
};
